package com.gc.cscott.cli;

import com.gc.cscott.domain.Competitor;
import com.gc.cscott.domain.Solution;
import com.gc.cscott.domain.contest.Contest;
import com.gc.cscott.domain.contest.Task;
import com.gc.cscott.domain.result.CompetitorResult;
import com.gc.cscott.fs.FileSystemWorker;
import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.io.FileUtils;
/**
 *
 * @author dmitry.mamonov
 */
public class GroupCompetitorsWithoutCompiledSourcesCommand implements Command {
    @Inject
    FileSystemWorker fsWorker;
    @Inject
    Contest contest;

    private static final String compilationRequired = "compilation_required";
    private static final String readyToBeTested = "ready_to_be_tested";


    public List<String> listHelp() {
        return ImmutableList.of(
                "Separates list of competitors into two parts.",
                "First - all-ok competitors.",
                "Second - competitors which has solutions without",
                "  corresponding executables",
                "Required argument:",
                "  <target directory>");
    }

    public List<String> perform(List<String> args) {
        LinkedList<String> mutableArgs = new LinkedList<String>(args);
        if (mutableArgs.isEmpty()) {
            throw new Error("Please specify target directory");
        } else {
            try {
                File targetDir = new File(mutableArgs.removeFirst());
                System.out.println("Competitors: ");
                List<Competitor> competitorsList = fsWorker.listCompetitorsAll();
                final int longestName = ReportUtils.longestName(competitorsList);
                for (Competitor competitor : competitorsList) {
                    boolean copyToOk = true;
                    for(Solution solution:fsWorker.listSolutions(competitor)){
                        try {
                            fsWorker.findExecutableInSolution(solution);
                        } catch(FileNotFoundException noSolution){
                            copyToOk = false;
                        }
                    }
                    System.out.printf("%-"+longestName+"s: %s", competitor.getName(), copyToOk?"Copy to "+readyToBeTested:"Copy to "+compilationRequired);
                    System.out.println();
                    if (copyToOk){
                        FileUtils.copyDirectory(competitor.getBasedir(), new File(new File(targetDir,readyToBeTested), competitor.getName()));
                    } else {
                        FileUtils.copyDirectory(competitor.getBasedir(), new File(new File(targetDir,compilationRequired), competitor.getName()));
                    }
                }
            } catch (IOException e) {
                throw new Error(e);
            }
        }
        return ImmutableList.copyOf(mutableArgs);
    }

}
